/*********************************************************
          File Name:10.33.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Mon 14 Mar 2016 08:19:12 PM CST
**********************************************************/

#include <iostream>
#include <iterator>
#include <fstream>
#include <algorithm>

int main(int argc, char *argv[])
{
    if(argc != 4)
    {
        std::cerr << argv[0] << " input output1 outpu2\n";
        std::cerr << "Example:\n"
            << "./a.out src.txt odd.txt even.txt\n";
        return 1;
    }

    std::ifstream ifile(argv[1]);
    if(!ifile.is_open())
    {
        std::cerr << " can't open " << argv[1] << std::endl;
        return 1;
    }

    std::istream_iterator<int> in(ifile), eof;

    if(in == eof)
    {
        std::cerr << "file `" << argv[1] << "` must only contain numbers\n";
        return 1;
    }

    std::ofstream ofile1(argv[2]), ofile2(argv[3]);
    std::ostream_iterator<int> odd(ofile1, " "), even(ofile2, "\n");

    for_each(in, eof, [&](int x) {
            if(x % 2 == 0)
            {
                even = x;
            }
            else
            {
                odd = x;
            }
            });

    ifile.close();
    ofile1.close();
    ofile2.close();
    return 0;
}
